package com.news.news.web;

import com.news.news.modle.Admin;
import com.news.news.service.LoginService;
import com.news.news.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by 罗铠威 on 2023/8/29 8:19
 * type:
 */
@Api(tags = "管理员登录控制器类的介绍")
@RestController
@RequestMapping(path = "/admin/loginCtl")
public class LoginController {
/*获得logger对象*/
    static Logger logger= LoggerFactory.getLogger(LoginController.class);

    @Resource
    LoginService loginService;
@ApiOperation(value = "前端登录验证账户密码是否正确",notes = "通过前端传递的账户account,密码password来向后端查询是否账户存在，若存在生成adminToken并返回状态码200。反之201直接返回")
@ApiImplicitParam(paramType = "body",name = "admin",value = "装在有账户与密码的对象")
@RequestMapping(value = "/login")
    public CommonResult test(@RequestBody Admin admin) {
//        logger.debug("打印方法信息：进入/admin/loginCtl/login方法，参数为admin={}",admin.toString());
//        logger.info("打印方法信息：进入/admin/loginCtl/login方法，参数为admin={}",admin.toString());
//        logger.warn("打印方法信息：进入/admin/loginCtl/login方法，参数为admin={}",admin.toString());
//        logger.error("打印方法信息：进入/admin/loginCtl/login方法，参数为admin={}",admin.toString());
        Admin login = loginService.login(admin);
//        System.out.println(login);
        CommonResult commonResult=null;
        if(login==null){
            commonResult= new CommonResult(201,null,"失败");
        }else {
            commonResult=new CommonResult(200,login,"成功");
        }
        return commonResult;
    }
    //
    @GetMapping(value = "/test")
    public CommonResult test(){
//        System.out.println("test[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[");
        return new CommonResult(200,null,"测试成功");
    }

    //验证当前密码是否正确
    @GetMapping(value = "/testpassword")
    public CommonResult testpassword(String curpwd,@RequestHeader("adminToken") String token){
       CommonResult commonResult=null;
     String password= loginService.testpassword(curpwd,token);
        if(password==null){
            commonResult= new CommonResult(201,null,"失败");
        }else {
            commonResult=new CommonResult(200,null,"成功");
        }
        return  commonResult;
    }
  //修改密码
  @GetMapping(value = "/updatepassword")
  public CommonResult updatepassword(String password,@RequestHeader("adminToken") String token){
      CommonResult commonResult=null;
     loginService.updatepassword(password,token);
      return  commonResult=new CommonResult(200,null,"密码修改成功");
  }
}
